﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using FPPrinter.Models.Doc;

namespace FPPrinter.Models.WpfDoc
{
    public static class ExWpf
    {
        
       static PageContent ToFixedPage( this IFPDocPage doc)
        {

            // Cast the Content property to a FixedPage
            var fixedPage = new FixedPage();
            fixedPage.Margin = new Thickness(0);
            

            PageContent pageContent = new PageContent();
            
            var Width = 96 * 8.27 - 0; // A4 height in inches multiplied by 96 dots per inch (DPI)
            var Height = 96 * 11.69 - 60; // A4 width in inches multiplied by 96 DPI

            pageContent.Width = Width;
            pageContent.Height = Height;
            
            ((IAddChild)pageContent).AddChild(fixedPage);

// Create a new Grid element
            var grid = new Grid();

// Set the height and width of the Grid

            grid.Height = Height;
            grid.Width =  Width;
            grid.Margin = new Thickness(10, 10, 10, 10);
            grid.ShowGridLines = true;
            // grid.Background = Brushes.Aqua;

// Add two rows to the Grid
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());

// Add the Grid to the FixedPage
            fixedPage.Children.Add(grid);

// Add an image to the first row of the Grid

            var imgs = doc.Images.ToList();
            for (int i = 0; i < 2; i++)
            {
                if (i> imgs.Count-1) break; // if there is only 1 image
                
                var image1 = new Image();
                image1.Source = new BitmapImage(new Uri(imgs[i].Url, UriKind.RelativeOrAbsolute));
                image1.Stretch = Stretch.Uniform;
                image1.Margin = new Thickness(10);
                grid.Children.Add(image1);
                image1.HorizontalAlignment = HorizontalAlignment.Center;

                Grid.SetRow(image1, i);
            }


 

            return pageContent;
        }
       static PageContent ToFixedPage_long( this IFPDocPage doc)
        {

            // Cast the Content property to a FixedPage
            var fixedPage = new FixedPage();
            fixedPage.Margin = new Thickness(0);
            

            PageContent pageContent = new PageContent();
            
            var Width = 96 * 8.27 - 0; // A4 height in inches multiplied by 96 dots per inch (DPI)
            var Height = 96 * 11.69 - 80; // A4 width in inches multiplied by 96 DPI

            pageContent.Width = Width;
            pageContent.Height = Height;
            
            ((IAddChild)pageContent).AddChild(fixedPage);

// Create a new Grid element
            var grid = new Grid();

// Set the height and width of the Grid

            grid.Height = Height;
            grid.Width =  Width;
            grid.Margin = new Thickness(10);
            grid.ShowGridLines = true;
            // grid.Background = Brushes.Aqua;

// Add two rows to the Grid
            // grid.RowDefinitions.Add(new RowDefinition());
            // grid.RowDefinitions.Add(new RowDefinition());

// Add the Grid to the FixedPage
            fixedPage.Children.Add(grid);

// Add an image to the first row of the Grid

            var imgs = doc.Images.ToList();
            for (int i = 0; i < 2; i++)
            {
                if (i> imgs.Count-1) break; // if there is only 1 image
                
                var image1 = new Image();
                image1.Source = new BitmapImage(new Uri(imgs[i].Url, UriKind.RelativeOrAbsolute));
                image1.Stretch = Stretch.Uniform;
                grid.Children.Add(image1);
                image1.HorizontalAlignment = HorizontalAlignment.Center;

                // Grid.SetRow(image1, i);
            }


 

            return pageContent;
        }

       
       /// <summary>
       /// to fixed doc
       /// </summary>
       /// <param name="doc"></param>
       /// <returns></returns>
        public static FixedDocument ToFixedDocWpf(this IOptimizedDoc doc)
        {

            if (doc is LongOptimizedDoc)
                return ToFixedDocWpf_long(doc);
            else 
                return ToFixedDocWpf_short(doc);

        }
       
       
         static FixedDocument ToFixedDocWpf_short(this IOptimizedDoc doc)
        {
            
            // Create a new FixedDocument
            var fixedDoc = new FixedDocument();

            var ps = new List<FixedDocument>();

            foreach (var page in doc.Pages)
            {
                var pageContent= ToFixedPage(page) ;
                fixedDoc.Pages.Add(pageContent);
            }

            return fixedDoc;
        }

         static FixedDocument ToFixedDocWpf_long(this IOptimizedDoc doc)
        {
            
            // Create a new FixedDocument
            var fixedDoc = new FixedDocument();

            foreach (var page in doc.Pages)
            {
                var pageContent= ToFixedPage_long(page) ;
                fixedDoc.Pages.Add(pageContent);
            }

            return fixedDoc;
        }

        
    }
}